# -*- coding:utf8 -*-
import time
#import os
from appium import webdriver
from appium.webdriver.common.touch_action import TouchAction

desired_caps = {}
desired_caps['appium-version'] = '1.0'
desired_caps['platformName'] = 'Android'
desired_caps['platformVersion'] = '6.0'
desired_caps['deviceName'] = 'Pixel 2 API 23'
desired_caps['newCommandTimeout'] = 8000
# desired_caps['automationName'] = 'UIAutomator2'
desired_caps['noReset'] = False
#desired_caps['app'] = '/Users/lichongyang/program/rearch/AndroidCoverage/apk/OpenSudoku.apk'
#desired_caps['app'] = '/Users/nju/Desktop/apk/apk/Ghost Commander File Manager_v1.53.10_apkpure.com.apk'

desired_caps['appPackage'] = 'com.ghostsq.commander'
desired_caps['appActivity']= '.FileCommander'

desired_caps["unicodeKeyboard"] = True
desired_caps["resetKeyboard"] = True

#driver = webdriver.Remote('http://0.0.0.0:4723/wd/hub', desired_caps)
driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', desired_caps)
driver.implicitly_wait(20)

time.sleep(5)


# test case 0:
el = driver.find_elements_by_id('com.ghostsq.commander:id/fld_name')[1]
el.click()


driver.back()

el = driver.find_elements_by_class_name('android.widget.Button')[0]
el.click()

driver.quit()

#driver = webdriver.Remote('http://0.0.0.0:4723/wd/hub', desired_caps)
driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', desired_caps)
driver.implicitly_wait(20)

# test case 0:
el = driver.find_elements_by_id('com.ghostsq.commander:id/fld_name')[1]
el.click()


driver.back()

el = driver.find_elements_by_class_name('android.widget.Button')[2]
el.click()

driver.quit()

#driver = webdriver.Remote('http://0.0.0.0:4723/wd/hub', desired_caps)
driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', desired_caps)
driver.implicitly_wait(20)

# test case 0:
el = driver.find_elements_by_id('com.ghostsq.commander:id/fld_name')[1]
el.click()


driver.back()

el = driver.find_elements_by_class_name('android.widget.Button')[1]
el.click()

driver.quit()

#driver = webdriver.Remote('http://0.0.0.0:4723/wd/hub', desired_caps)
driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', desired_caps)
driver.implicitly_wait(20)

# test case1: 帮助
el = driver.find_element_by_id("com.ghostsq.commander:id/F1")
el.click()

el = driver.find_element_by_id("android:id/button1")
el.click()

# test case2: 设置，左面板设置，取消所有勾选，排序方式选择按类型，返回主页面
el = driver.find_element_by_id("com.ghostsq.commander:id/F9")
el.click()

el = driver.find_elements_by_id("android:id/title")[1]
el.click()

el = driver.find_elements_by_id("android:id/checkbox")[0]
el.click()

el = driver.find_elements_by_id("android:id/checkbox")[1]
el.click()

el = driver.find_elements_by_id("android:id/checkbox")[2]
el.click()

el = driver.find_elements_by_class_name("android.widget.RelativeLayout")[2]
el.click()

el = driver.find_elements_by_id("android:id/text1")[1]
el.click()

driver.press_keycode(4)

time.sleep(5)

driver.press_keycode(4)

# test case3: 设置，左面板设置，点击所有勾选，排序方式选择按大小，返回主页面
el = driver.find_element_by_id("com.ghostsq.commander:id/F9")
el.click()

el = driver.find_elements_by_id("android:id/title")[1]
el.click()

el = driver.find_elements_by_id("android:id/checkbox")[0]
el.click()

el = driver.find_elements_by_id("android:id/checkbox")[1]
el.click()

el = driver.find_elements_by_id("android:id/checkbox")[2]
el.click()

el = driver.find_elements_by_class_name("android.widget.RelativeLayout")[2]
el.click()

el = driver.find_elements_by_id("android:id/text1")[2]
el.click()

driver.press_keycode(4)

time.sleep(5)

driver.press_keycode(4)

# test case4: 设置，左面板设置，排序方式选择按日期，返回主页面
el = driver.find_element_by_id("com.ghostsq.commander:id/F9")
el.click()

el = driver.find_elements_by_id("android:id/title")[1]
el.click()

el = driver.find_elements_by_class_name("android.widget.RelativeLayout")[2]
el.click()

el = driver.find_elements_by_id("android:id/text1")[3]
el.click()

driver.press_keycode(4)

time.sleep(5)

driver.press_keycode(4)

# test case5: 设置，右面板设置，取消所有勾选，排序方式选择按类型，返回主页面
el = driver.find_element_by_id("com.ghostsq.commander:id/F9")
el.click()

el = driver.find_elements_by_id("android:id/title")[2]
el.click()

el = driver.find_elements_by_id("android:id/checkbox")[0]
el.click()

el = driver.find_elements_by_id("android:id/checkbox")[1]
el.click()

el = driver.find_elements_by_id("android:id/checkbox")[2]
el.click()

el = driver.find_elements_by_class_name("android.widget.RelativeLayout")[2]
el.click()

el = driver.find_elements_by_id("android:id/text1")[1]
el.click()

driver.press_keycode(4)

time.sleep(5)

driver.press_keycode(4)

# test case6: 设置，右面板设置，点击所有勾选，排序方式选择按大小，返回主页面
el = driver.find_element_by_id("com.ghostsq.commander:id/F9")
el.click()

el = driver.find_elements_by_id("android:id/title")[2]
el.click()

el = driver.find_elements_by_id("android:id/checkbox")[0]
el.click()

el = driver.find_elements_by_id("android:id/checkbox")[1]
el.click()

el = driver.find_elements_by_id("android:id/checkbox")[2]
el.click()

el = driver.find_elements_by_class_name("android.widget.RelativeLayout")[2]
el.click()

el = driver.find_elements_by_id("android:id/text1")[2]
el.click()

driver.press_keycode(4)

time.sleep(5)

driver.press_keycode(4)

time.sleep(3)

# test case7: 设置，右面板设置，排序方式选择按日期，返回主页面
el = driver.find_element_by_id("com.ghostsq.commander:id/F9")
el.click()

el = driver.find_elements_by_id("android:id/title")[2]
el.click()

el = driver.find_elements_by_class_name("android.widget.RelativeLayout")[2]
el.click()

el = driver.find_elements_by_id("android:id/text1")[3]
el.click()

driver.press_keycode(4)
driver.press_keycode(4)

# test case8: 设置，取消大小写排序，返回主页面，再次进入设置，恢复大小写排序
el = driver.find_element_by_id("com.ghostsq.commander:id/F9")
el.click()

el = driver.find_elements_by_id("android:id/checkbox")[0]
el.click()

driver.press_keycode(4)

time.sleep(2)

el = driver.find_element_by_id("com.ghostsq.commander:id/F9")
el.click()

el = driver.find_elements_by_id("android:id/checkbox")[0]
el.click()

driver.press_keycode(4)

# test case9: 设置，并排面板设置，Yes，返回主页面，再次进入，设置并排面板为No，返回主页面，再次进入，设置并排面板为auto
el = driver.find_element_by_id("com.ghostsq.commander:id/F9")
el.click()

el = driver.find_elements_by_id("android:id/title")[5]
el.click()

el = driver.find_element_by_id("android:id/button2")
el.click()

el = driver.find_elements_by_id("android:id/title")[5]
el.click()

el = driver.find_elements_by_id("android:id/text1")[0]
el.click()

driver.press_keycode(4)

el = driver.find_element_by_id("com.ghostsq.commander:id/F9")
el.click()

el = driver.find_elements_by_id("android:id/title")[5]
el.click()

el = driver.find_elements_by_id("android:id/text1")[1]
el.click()

driver.press_keycode(4)

el = driver.find_element_by_id("com.ghostsq.commander:id/F9")
el.click()

el = driver.find_elements_by_id("android:id/title")[5]
el.click()

el = driver.find_elements_by_id("android:id/text1")[2]
el.click()

driver.press_keycode(4)

# test case10: 设置，勾选分行显示，返回主页面，再次进入，取消勾选，返回主页面
el = driver.find_element_by_id("com.ghostsq.commander:id/F9")
el.click()

el = driver.find_elements_by_id("android:id/checkbox")[1]
el.click()

driver.press_keycode(4)

el = driver.find_element_by_id("com.ghostsq.commander:id/F9")
el.click()

el = driver.find_elements_by_id("android:id/checkbox")[1]
el.click()

driver.press_keycode(4)

# test case11: 设置，触屏优化，Yes，返回主页面，再次进入，设置触屏优化为No，返回主页面，再次进入，设置触屏优化为auto
el = driver.find_element_by_id("com.ghostsq.commander:id/F9")
el.click()

el = driver.find_elements_by_id("android:id/title")[7]
el.click()

el = driver.find_element_by_id("android:id/button2")
el.click()

el = driver.find_elements_by_id("android:id/title")[7]
el.click()

el = driver.find_elements_by_id("android:id/text1")[1]
el.click()

driver.press_keycode(4)

el = driver.find_element_by_id("com.ghostsq.commander:id/F9")
el.click()

el = driver.find_elements_by_id("android:id/title")[7]
el.click()

el = driver.find_elements_by_id("android:id/text1")[2]
el.click()

driver.press_keycode(4)

el = driver.find_element_by_id("com.ghostsq.commander:id/F9")
el.click()

el = driver.find_elements_by_id("android:id/title")[7]
el.click()

el = driver.find_elements_by_id("android:id/text1")[0]
el.click()

driver.press_keycode(4)

# test case12: 设置，取消启用图标勾选，返回主页面，再次进入，勾选启用图标，返回主页面
el = driver.find_element_by_id("com.ghostsq.commander:id/F9")
el.click()

el = driver.find_elements_by_id("android:id/checkbox")[2]
el.click()

driver.press_keycode(4)

el = driver.find_element_by_id("com.ghostsq.commander:id/F9")
el.click()

el = driver.find_elements_by_id("android:id/checkbox")[2]
el.click()

driver.press_keycode(4)

# test case13: 设置，更多选项，导出设置，导入设置，导出日志
el = driver.find_element_by_id("com.ghostsq.commander:id/F9")
el.click()

time.sleep(2)

el = driver.find_element_by_accessibility_id("More options")
el.click()

time.sleep(2)

el = driver.find_elements_by_id("android:id/title")[0]
el.click()

el = driver.find_element_by_accessibility_id("More options")
el.click()

time.sleep(2)

el = driver.find_elements_by_id("android:id/title")[1]
el.click()

el = driver.find_element_by_id("com.ghostsq.commander:id/F9")
el.click()

time.sleep(2)

el = driver.find_element_by_accessibility_id("More options")
el.click()

time.sleep(2)

el = driver.find_elements_by_id("android:id/title")[2]
el.click()

driver.press_keycode(4)

# test case14: 主页面，切换两个面板
el = driver.find_element_by_id("com.ghostsq.commander:id/right_dir")
el.click()

el = driver.find_element_by_id("com.ghostsq.commander:id/left_dir")
el.click()



# test case16: 在当前文件夹下，按不同类型进行排序
el = driver.find_element_by_id("com.ghostsq.commander:id/action_sort")
el.click()

el = driver.find_elements_by_id("android:id/title")[1]
el.click()

el = driver.find_element_by_id("com.ghostsq.commander:id/action_sort")
el.click()

el = driver.find_elements_by_id("android:id/title")[2]
el.click()

el = driver.find_element_by_id("com.ghostsq.commander:id/action_sort")
el.click()

el = driver.find_elements_by_id("android:id/title")[3]
el.click()

el = driver.find_element_by_id("com.ghostsq.commander:id/action_sort")
el.click()

el = driver.find_elements_by_id("android:id/title")[0]
el.click()

# test case17: 在当前文件夹下，选择文件，选择文件夹
el = driver.find_element_by_id("com.ghostsq.commander:id/action_select")
el.click()

el = driver.find_elements_by_id("android:id/title")[0]
el.click()

time.sleep(5)

el = driver.find_element_by_id("com.ghostsq.commander:id/for_files")
el.click()

el = driver.find_element_by_id("android:id/button1")
el.click()

# test case20: 在当前文件夹下，更多选项，新建，新建文件夹
#os.system("adb shell rmdir /storage/emulated/0/.aa")
#os.system("adb shell rmdir /storage/emulated/0/.aatest")
el = driver.find_elements_by_id('com.ghostsq.commander:id/fld_name')[1]
el.click()

time.sleep(2)

el = driver.find_element_by_accessibility_id("More options")
el.click()

time.sleep(2)

el = driver.find_elements_by_id("android:id/title")[1]
el.click()

el = driver.find_elements_by_id("android:id/title")[0]
el.click()

el = driver.find_element_by_id("com.ghostsq.commander:id/edit_field")
el.send_keys(".aa")

el = driver.find_element_by_id("android:id/button1")
el.click()

time.sleep(2)
# test case21: 在当前文件夹下，更多选项，新建，新建文件
#os.system("adb shell rm /storage/emulated/0/aabb.txt")
el = driver.find_element_by_accessibility_id("More options")
el.click()

time.sleep(2)

el = driver.find_elements_by_id("android:id/title")[1]
el.click()

el = driver.find_elements_by_id("android:id/title")[1]
el.click()

el = driver.find_element_by_id("com.ghostsq.commander:id/edit_field")
el.send_keys("aabb")

el = driver.find_element_by_id("android:id/button1")
el.click()

time.sleep(2)

driver.back()
# test case18: 在选择文件夹的情况下，进行重命名，查看，编辑操作
el = driver.find_element_by_id("com.ghostsq.commander:id/F2")
el.click()

el = driver.find_element_by_id("android:id/button2")
el.click()

el = driver.find_element_by_id("com.ghostsq.commander:id/F2")
el.click()

el = driver.find_element_by_id("com.ghostsq.commander:id/edit_field")
el.send_keys("test")

el = driver.find_element_by_id("android:id/button1")
el.click()

time.sleep(5)

el = driver.find_element_by_id("com.ghostsq.commander:id/F3")
el.click()

el = driver.find_element_by_id("android:id/button1")
el.click()

el = driver.find_element_by_id("com.ghostsq.commander:id/F4")
el.click()

el = driver.find_element_by_id("android:id/button1")
el.click()

# test case19: 在选择文件的情况下取消勾选
el = driver.find_element_by_id("com.ghostsq.commander:id/action_select")
el.click()

el = driver.find_elements_by_id("android:id/title")[1]
el.click()

time.sleep(5)

el = driver.find_element_by_id("com.ghostsq.commander:id/for_files")
el.click()

el = driver.find_element_by_id("android:id/button1")
el.click()



# test case22: 在当前文件夹下，更多选项，新建，新建zip文件
el = driver.find_element_by_accessibility_id("More options")
el.click()

time.sleep(2)

el = driver.find_elements_by_id("android:id/title")[1]
el.click()

el = driver.find_elements_by_id("android:id/title")[2]
el.click()

el = driver.find_element_by_id("android:id/button1")
el.click()



time.sleep(5)

# test case23: 在当前文件夹下，更多选项，应用程序，切换面板模式
el = driver.find_element_by_accessibility_id("More options")
el.click()

el = driver.find_elements_by_id("android:id/title")[2]
el.click()

el = driver.find_elements_by_id("android:id/title")[0]
el.click()

el = driver.find_element_by_accessibility_id("More options")
el.click()

el = driver.find_elements_by_id("android:id/title")[2]
el.click()

el = driver.find_elements_by_id("android:id/title")[0]
el.click()

# test case24: 在当前文件夹下，更多选项，应用程序，键盘
el = driver.find_element_by_accessibility_id("More options")
el.click()

el = driver.find_elements_by_id("android:id/title")[2]
el.click()

el = driver.find_elements_by_id("android:id/title")[1]
el.click()

el = driver.find_elements_by_class_name("android.widget.ImageButton")[0]
el.click()

el = driver.find_elements_by_id("android:id/title")[2]
el.click()

el = driver.find_elements_by_id("android:id/title")[1]
el.click()

# test case25: 在当前文件夹下，更多选项，应用程序，设置
el = driver.find_element_by_accessibility_id("More options")
el.click()

el = driver.find_elements_by_id("android:id/title")[2]
el.click()

el = driver.find_elements_by_id("android:id/title")[3]
el.click()

driver.press_keycode(4)

# test case26: 在当前文件夹下，更多选项，帮助，信息
el = driver.find_element_by_accessibility_id("More options")
el.click()

el = driver.find_elements_by_id("android:id/title")[3]
el.click()

el = driver.find_elements_by_id("android:id/title")[0]
el.click()

el = driver.find_element_by_id("android:id/button1")
el.click()

time.sleep(2)

# test case27: 在当前文件夹下，更多选项，帮助，在线帮助
el = driver.find_element_by_accessibility_id("More options")
el.click()

el = driver.find_elements_by_id("android:id/title")[3]
el.click()

el = driver.find_elements_by_id("android:id/title")[1]
el.click()

time.sleep(5)
driver.press_keycode(4)
time.sleep(5)

# test case28: 在当前文件夹下，更多选项，帮助，关于
el = driver.find_element_by_accessibility_id("More options")
el.click()

el = driver.find_elements_by_id("android:id/title")[3]
el.click()

el = driver.find_elements_by_id("android:id/title")[2]
el.click()

el = driver.find_element_by_id("android:id/button1")
el.click()

# test case29: 在当前文件夹下，更多选项，位置，主页
el = driver.find_element_by_accessibility_id("More options")
el.click()

el = driver.find_elements_by_id("android:id/title")[0]
el.click()

el = driver.find_elements_by_id("android:id/title")[0]
el.click()

# test case30: 在当前文件夹下，更多选项，位置，书签
el = driver.find_element_by_accessibility_id("More options")
el.click()

el = driver.find_elements_by_id("android:id/title")[0]
el.click()

el = driver.find_elements_by_id("android:id/title")[1]
el.click()

# test case31: 在当前文件夹下，更多选项，位置，改变位置
el = driver.find_element_by_accessibility_id("More options")
el.click()

el = driver.find_elements_by_id("android:id/title")[0]
el.click()

el = driver.find_elements_by_id("android:id/title")[2]
el.click()

el = driver.find_element_by_id("com.ghostsq.commander:id/go_button")
el.click()

# test case32: 在当前文件夹下，更多选项，位置，同步面板
el = driver.find_element_by_accessibility_id("More options")
el.click()

el = driver.find_elements_by_id("android:id/title")[0]
el.click()

el = driver.find_elements_by_id("android:id/title")[3]
el.click()

# test case33: 在当前文件夹下，更多选项，位置，搜索
el = driver.find_element_by_accessibility_id("More options")
el.click()

el = driver.find_elements_by_id("android:id/title")[0]
el.click()

el = driver.find_elements_by_id("android:id/title")[4]
el.click()

el = driver.find_element_by_id("android:id/button1")
el.click()

# test case34: 在当前文件夹下，页首，页尾，回到主页
el = driver.find_element_by_id("com.ghostsq.commander:id/action_nav")
el.click()

el = driver.find_elements_by_id("android:id/title")[1]
el.click()

el = driver.find_element_by_id("com.ghostsq.commander:id/action_nav")
el.click()

el = driver.find_elements_by_id("android:id/title")[2]
el.click()

el = driver.find_element_by_id("com.ghostsq.commander:id/action_nav")
el.click()

el = driver.find_elements_by_id("android:id/title")[3]
el.click()



driver.quit()
